package fr.upstlse.glre.client.administration.actualite;

import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.administration.actualite.action.CreationActualiteAction;
import fr.upstlse.glre.client.administration.component.Center;
import fr.upstlse.glre.client.constante.RfgConstanteEnum;
import fr.upstlse.glre.client.constante.fields.ActualiteConstante;
import fr.upstlse.glre.client.widgets.fields.ActualiteFields;

public class CreationActualitePanel extends Composite {

	private ContentPanel creationPanel;

	private ActualiteFields actualiteFields;
	private FormPanel formPanel;
	
	private static CreationActualitePanel instance;
	
	public CreationActualitePanel() {
		CreationActualitePanel.setInstance(this);
		
		createCreationPanel();
		createCreationForm();

		creationPanel.add(formPanel);
		initWidget(creationPanel);
	}

	private void createCreationPanel() {
		creationPanel = new ContentPanel();
		creationPanel.setAutoWidth(true);
		creationPanel.setWidth(500);
		creationPanel.setId(ActualiteConstante.CREATION_PANEL_ID.getValue());
		creationPanel.setBorders(true);
		creationPanel.setCollapsible(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(creationPanel, creationPanel
				.getHeader());
		lDraggable.setContainer(Center.getInstance());

		creationPanel
				.setHeading(ActualiteConstante.CREATION_PANEL_HEADER_TEXT.getValue());
	}

	public void createCreationForm() {
		formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setAutoWidth(true);

		actualiteFields = new ActualiteFields(null, formPanel);

		Button lSubmit = new Button(
				RfgConstanteEnum.CREATION_SUBMIT.getValue());
		lSubmit.addSelectionListener(new CreationActualiteAction());
		formPanel.addButton(lSubmit);
	}

	public ContentPanel getCreationPanel() {
		return creationPanel;
	}

	public void setCreationPanel(ContentPanel pCreationPanel) {
		creationPanel = pCreationPanel;
	}

	public ActualiteFields getActualiteFields() {
		return actualiteFields;
	}

	public void setActualiteFields(ActualiteFields pActualiteFields) {
		actualiteFields = pActualiteFields;
	}

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setFormPanel(FormPanel pFormPanel) {
		formPanel = pFormPanel;
	}

	public static CreationActualitePanel getInstance() {
		if(instance == null) {
			instance = new CreationActualitePanel();
		}
		return instance;
	}

	public static void setInstance(CreationActualitePanel pInstance) {
		instance = pInstance;
	}
}
